import request from "@/utils/request";

export interface BaseSaleAttrData {
  id: number;
  name: string;
}

interface SpuSaleAttrValueData {
  id?: number;
  spuId?: number;
  baseSaleAttrId: number | string;
  saleAttrValueName: string;
  saleAttrName?: string;
  isChecked?: null;
}

export interface SpuSaleAttrData {
  id?: number;
  spuId?: number;
  baseSaleAttrId: number | string;
  saleAttrName: string;
  spuSaleAttrValueList: SpuSaleAttrValueData[];
  isEdit?: boolean;
  saleAttrValueName?: string;
  saleAttrIdValueId?: string;
}

export interface ResponseData {
  data: string;
}

export interface SpuImageData {
  id?: number;
  spuId?: number;
  imgName: string | undefined;
  imgUrl: string | undefined;
  url?: string;
  name?: string;
  response?: ResponseData;
  isDefault?: string;
}
export type SpuData = {
  id?: number;
  spuName: string;
  description: string;
  category3Id: number | string;
  tmId: number | string;
  spuSaleAttrList: SpuSaleAttrData[];
  spuImageList: SpuImageData[];
};

type SpuPageListData = {
  records: SpuData[];
  total: number;
  size: number;
  current: number;
  searchCount: boolean;
  pages: number;
};

export default {
  // 获取所有的基础销售属性列表
  // GET /admin/product/baseSaleAttrList
  // getBaseSaleAttrList
  getBaseSaleAttrList() {
    return request.get<any, BaseSaleAttrData[]>(
      "/admin/product/baseSaleAttrList"
    );
  },

  // DELETE /admin/product/deleteSpu/{spuId}
  // deleteSpu
  deleteSpu(spuId: number) {
    return request.delete<any, null>(`/admin/product/deleteSpu/${spuId}`);
  },

  //  通过关键紫搜索相应的spu信息
  // GET /admin/product/findSpuInfoByKeyword/{keyword}
  // findSpuInfoByKeyword

  // GET /admin/product/getSpuById/{spuId}
  // 根据spu的id，获取spu基本信息
  getSpuById(spuId: number) {
    return request.get<any, SpuData>(`/admin/product/getSpuById/${spuId}`);
  },

  // POST /admin/product/inner/findSpuInfoBySpuIdList
  // findSpuInfoBySpuIdList

  // POST /admin/product/saveSpuInfo
  // saveSpuInfo
  // POST /admin/product/updateSpuInfo
  // 更改spu信息
  addOrUpdate(spu: SpuData) {
    return request.post<any, null>(
      `/admin/product/${spu.id ? "update" : "save"}SpuInfo`,
      spu
    );
  },
  // GET /admin/product/{page}/{limit}
  getPageList(page: number, limit: number, category3Id: number) {
    return request.get<any, SpuPageListData>(
      `/admin/product/${page}/${limit}?category3Id=${category3Id}`
    );
  },
};
